package gui;

import facade.CarInfo;
import facade.RoadInfo;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.List;


public class DisplayedRoad extends DisplayedTrafficInfrastructure {

	private final RoadInfo road;
	
	public DisplayedRoad(RoadInfo r) {
		this.road = r;
	}
	
	private double getRoadDirection(){
		Line2D pos = road.position;
		double angle = Math.atan((pos.getY2() - pos.getY1())/(pos.getX2()-pos.getX1())); //geeft altijd hoek tssn -90 en +90
		if(pos.getX2() < pos.getX1()) //wanneer in 2e of 3e kwadrant moet zijn
			return angle + Math.PI;
		else
			return angle;
	}
	
	@Override
	public void draw(Graphics2D g2d) {
		List<CarInfo> cars = road.cars;
		AffineTransform savedTransform = g2d.getTransform();
		
		int nbSegments = road.length;
		double theta = getRoadDirection();
		
		g2d.translate(road.position.getX1(), road.position.getY1());
		g2d.rotate(theta);
			
		double segmentLength = (road.position.getP1().distance(road.position.getP2())) / nbSegments;
		
		Line2D topArrowLine = new Line2D.Double(0,0,0.25,0.25);
		Line2D bottomArrowLine = new Line2D.Double(0.25,0.25,0,0.5);
		
		for(int i = 0; i < nbSegments; i++) {
			//argvolgorde: linksbovenhoek x en y; breedte, hoogte
			Rectangle2D rect = new Rectangle2D.Double(0,0,segmentLength,0.5);
			
			g2d.draw(rect);
			if(i == 0) {
				g2d.draw(topArrowLine);
				g2d.draw(bottomArrowLine);
			}
			
			drawCar(g2d, cars.get(i), rect, new Point2D.Double(segmentLength/2,0.25));
			
			g2d.translate(segmentLength, 0);
		}
		
		g2d.draw(topArrowLine);
		g2d.draw(bottomArrowLine);
		
		g2d.setTransform(savedTransform);
	}

}
